package com.ruoyi.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.vo.BusNeighborhoodVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.BusNeighborhoodLedger;
import com.ruoyi.business.service.IBusNeighborhoodLedgerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 小区台账Controller
 *
 * @author ling
 * @date 2024-07-12
 */
@RestController
@RequestMapping("/business/ledger")
public class BusNeighborhoodLedgerController extends BaseController
{
    @Autowired
    private IBusNeighborhoodLedgerService busNeighborhoodLedgerService;
    @Value("${export.max:10000}")
    private Integer max;

    /**
     * 查询小区台账列表
     */
//    @PreAuthorize("@ss.hasPermi('business:neighborhoodledger:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(BusNeighborhoodLedger busNeighborhoodLedger)
//    {
//        startPage();
//        List<BusNeighborhoodLedger> list = busNeighborhoodLedgerService.selectBusNeighborhoodLedgerListAB(busNeighborhoodLedger);
//        list.forEach(System.out::println);
//        return getDataTable(list);
//    }

    /**
     * 查询小区台账列表
     */
//     获取调压设施数量、缸数量、闸数量、楼栋数
    @PreAuthorize("@ss.hasPermi('business:ledger:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusNeighborhoodLedger busNeighborhoodLedger)
    {
        startPage();
        List<BusNeighborhoodVo> list = busNeighborhoodLedgerService.selectBusNeighborhoodLedgerListAB(busNeighborhoodLedger);
        System.out.println("======================================================================");
        list.forEach(System.out::println);
        return getDataTable(list);
    }


    /**
     * 导出小区台账列表
     */
    @PreAuthorize("@ss.hasPermi('business:ledger:export')")
    @Log(title = "小区台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusNeighborhoodLedger busNeighborhoodLedger)
    {
        List<BusNeighborhoodLedger> list = busNeighborhoodLedgerService.selectBusNeighborhoodLedgerList(busNeighborhoodLedger);
        if (list.size() > max) {
            throw new ServiceException("导出失败！,每次最多导出 " + max + "条");
        }
        ExcelUtil<BusNeighborhoodLedger> util = new ExcelUtil<BusNeighborhoodLedger>(BusNeighborhoodLedger.class);
        util.exportExcel(response, list, "小区台账数据");
    }

    /**
     * 获取小区台账详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:ledger:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(busNeighborhoodLedgerService.selectBusNeighborhoodLedgerById(id));
    }

    /**
     * 新增小区台账
     */
    @PreAuthorize("@ss.hasPermi('business:ledger:add')")
    @Log(title = "小区台账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusNeighborhoodLedger busNeighborhoodLedger)
    {
        return toAjax(busNeighborhoodLedgerService.insertBusNeighborhoodLedger(busNeighborhoodLedger));
    }

    /**
     * 修改小区台账
     */
    @PreAuthorize("@ss.hasPermi('business:ledger:edit')")
    @Log(title = "小区台账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusNeighborhoodLedger busNeighborhoodLedger)
    {
        return toAjax(busNeighborhoodLedgerService.updateBusNeighborhoodLedger(busNeighborhoodLedger));
    }

    /**
     * 删除小区台账
     */
    @PreAuthorize("@ss.hasPermi('business:ledger:remove')")
    @Log(title = "小区台账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(busNeighborhoodLedgerService.deleteBusNeighborhoodLedgerByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('business:ledger:getVatAndBrake')")
    @GetMapping("/VatAndBrake")
    public AjaxResult getVatAndBrake(BusNeighborhoodLedger busNeighborhoodLedger)
    {
//        传递小区id
// 通过小区id，查询对应的缸与闸的数量         缸数量和闸数量通过设备设施管理里面选择小区统计出来的

        List<BusNeighborhoodLedger> list = busNeighborhoodLedgerService.selectBusNeighborhoodLedgerList(busNeighborhoodLedger);

        return success(list);
    }
}
